﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2008-11-03 23:13:11Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Reflection;
using Outcoder.Concurrency;

namespace Outcoder
{
	/// <summary>
	/// Use to invoke an asynchronous WCF service method synchronously.
	/// </summary>
	public delegate IAsyncResult BeginAction(AsyncCallback asyncResult, object state);
	public delegate IAsyncResult BeginAction<TActionArgument1>(TActionArgument1 argument1, AsyncCallback asyncResult, object state);
	public delegate IAsyncResult BeginAction<TActionArgument1, TActionArgument2>(TActionArgument1 argument1, TActionArgument2 argument2, AsyncCallback asyncResult, object state);
	public delegate IAsyncResult BeginAction<TActionArgument1, TActionArgument2, TActionArgument3>(TActionArgument1 argument1, TActionArgument2 argument2, TActionArgument3 argument3, AsyncCallback asyncResult, object state);
	public delegate IAsyncResult BeginAction<TActionArgument1, TActionArgument2, TActionArgument3, TActionArgument4>(TActionArgument1 argument1, TActionArgument2 argument2, TActionArgument3 argument3, TActionArgument4 argument4, AsyncCallback asyncResult, object state);
	public delegate void EndAction(IAsyncResult asyncResult);
	public delegate TReturn EndAction<TReturn>(IAsyncResult asyncResult);

	/// <summary>
	/// Enables one to call asynchronous WCF services, 
	/// via generated proxies, in a synchronous manner.
	/// </summary>
	public static class SynchronousChannelBroker
	{
		const string synchronousCallInvalidMessage = "Synchronous WCF call can not be performed from UI Thread.";

		static void EnsureNonUIThread()
		{
			if (!UISynchronizationContext.Instance.InvokeRequired)
			{
				throw new ConcurrencyException(synchronousCallInvalidMessage);
			}
		}

		#region Return type specified
		/// <summary>
		/// Performs the action synchronously.
		/// </summary>
		/// <exception cref="ConcurrencyException">Occurs if the call is executed from the UI thread.</exception>
		/// <exception cref="TargetInvocationException">Occurs if the specified <code>endAction</code> 
		/// throws a <code>TargetInvocationException</code></exception>
		/// <typeparam name="TReturn">The type of the object returned by the specified <code>endAction</code>.</typeparam>
		/// <param name="beginAction">The Begin[MethodName] method.</param>
		/// <param name="endAction">The End[MethodName] method.</param>
		/// <returns>The result of the <code>EndAction</code> method.</returns>
		/// <example>
		///	try
		///	{
		///		/* Perform synchronous WCF call. */
		///		result = SynchronousChannelBroker.PerformAction<string>(
		///			someService.BeginGetString, someService.EndGetString);
		///	}
		///	catch (TargetInvocationException ex)
		///	{
		///		DisplayMessage(string.Format("Unable to communicate with server. {0} {1}", 
		///			ex.Message, ex.StackTrace));
		///	}
		/// </example>
		public static TReturn PerformAction<TReturn>(BeginAction beginAction, EndAction<TReturn> endAction)
		{
			EnsureNonUIThread();
			var beginResult = beginAction(null, null);
			var result = endAction(beginResult);
			return result;
		}

		/// <summary>
		/// Performs the action synchronously.
		/// </summary>
		/// <exception cref="ConcurrencyException">Occurs if the call is executed from the UI thread.</exception>
		/// <exception cref="TargetInvocationException">Occurs if the specified <code>endAction</code> 
		/// throws a <code>TargetInvocationException</code></exception>
		/// <typeparam name="TReturn">The type of the object returned 
		/// by the specified <code>endAction</code>.</typeparam>
		/// <typeparam name="TActionArgument1">The type of the argument required 
		/// by the specified <code>BeginAction</code>.</typeparam>
		/// <param name="beginAction">The Begin[MethodName] method.</param>
		/// <param name="endAction">The End[MethodName] method.</param>
		/// <param name="argument1">The argument required by the specified <code>BeginAction</code></param>
		/// <returns>The result of the <code>EndAction</code> method.</returns>
		/// <example>
		///	try
		///	{
		///		/* Perform synchronous WCF call. */
		///		result = SynchronousChannelBroker.PerformAction&lt;string, string&gt;(
		///			someService.BeginGetString, someService.EndGetString, "test string");
		///	}
		///	catch (TargetInvocationException ex)
		///	{
		///		DisplayMessage(string.Format("Unable to communicate with server. {0} {1}", 
		///			ex.Message, ex.StackTrace));
		///	}
		/// </example>
		public static TReturn PerformAction<TReturn, TActionArgument1>(
			Func<TActionArgument1, AsyncCallback, object, IAsyncResult> beginAction, 
			Func<IAsyncResult, TReturn> endAction, TActionArgument1 argument1)
		{
			EnsureNonUIThread();
			var beginResult = beginAction(argument1, null, null);
			var result = endAction(beginResult);
			return result;
		}

		/// <summary>
		/// Performs the action synchronously.
		/// </summary>
		/// <exception cref="ConcurrencyException">Occurs if the call is executed from the UI thread.</exception>
		/// <exception cref="TargetInvocationException">Occurs if the specified <code>endAction</code> 
		/// throws a <code>TargetInvocationException</code></exception>
		/// <typeparam name="TReturn">The type of the object returned 
		/// by the specified <code>endAction</code>.</typeparam>
		/// <typeparam name="TActionArgument1">The type of the first argument required 
		/// by the specified <code>BeginAction</code>.</typeparam>
		/// <typeparam name="TActionArgument2">The type of the second argument required 
		/// by the specified <code>BeginAction</code>.</typeparam>
		/// <param name="beginAction">The Begin[MethodName] method.</param>
		/// <param name="endAction">The End[MethodName] method.</param>
		/// <param name="argument1">The first argument required by the specified <code>BeginAction</code></param>
		/// <param name="argument2">The second argument required by the specified <code>BeginAction</code></param>
		/// <returns>The result of the <code>EndAction</code> method.</returns>
		/// <example>
		/// string result;
		///	try
		///	{
		///		/* Perform synchronous WCF call. */
		///		result = SynchronousChannelBroker.PerformAction&lt;string, string, int&gt;(
		///			someService.BeginGetString, someService.EndGetString, "test string", 5);
		///	}
		///	catch (TargetInvocationException ex)
		///	{
		///		DisplayMessage(string.Format("Unable to communicate with server. {0} {1}", 
		///			ex.Message, ex.StackTrace));
		///	}
		/// </example>
		public static TReturn PerformAction<TReturn, TActionArgument1, TActionArgument2>(
			BeginAction<TActionArgument1, TActionArgument2> beginAction, 
			EndAction<TReturn> endAction, 
			TActionArgument1 argument1, TActionArgument2 argument2)
		{
			EnsureNonUIThread();
			var beginResult = beginAction(argument1, argument2, null, null);
			var result = endAction(beginResult);
			return result;
		}

		public static TReturn PerformAction<TReturn, TActionArgument1, TActionArgument2, TActionArgument3>(
			BeginAction<TActionArgument1, TActionArgument2, TActionArgument3> action, 
			EndAction<TReturn> endAction, 
			TActionArgument1 argument1, TActionArgument2 argument2, TActionArgument3 argument3)
		{
			EnsureNonUIThread();
			var beginResult = action(argument1, argument2, argument3, null, null);
			var result = endAction(beginResult);
			return result;
		}

		public static TReturn PerformAction<TReturn, TActionArgument1, TActionArgument2, TActionArgument3, TActionArgument4>(
			BeginAction<TActionArgument1, TActionArgument2, TActionArgument3, TActionArgument4> action, 
			EndAction<TReturn> endAction, 
			TActionArgument1 argument1, TActionArgument2 argument2, TActionArgument3 argument3, TActionArgument4 argument4)
		{
			EnsureNonUIThread();
			var beginResult = action(argument1, argument2, argument3, argument4, null, null);
			var result = endAction(beginResult);
			return result;
		}
		#endregion

		#region void return type
		/// <summary>
		/// Performs the action synchronously.
		/// </summary>
		/// <exception cref="ConcurrencyException">Occurs if the call is executed from the UI thread.</exception>
		/// <exception cref="TargetInvocationException">Occurs if the specified <code>endAction</code> 
		/// throws a <code>TargetInvocationException</code></exception>
		/// <param name="beginAction">The Begin[MethodName] method.</param>
		/// <param name="endAction">The End[MethodName] method.</param>
		/// <returns>The result of the <code>EndAction</code> method.</returns>
		/// <example>
		///	try
		///	{
		///		/* Perform synchronous WCF call. */
		///		SynchronousChannelBroker.PerformAction&lt;string&gt;(
		///			someService.BeginGetString, someService.EndGetString, "test string");
		///	}
		///	catch (TargetInvocationException ex)
		///	{
		///		DisplayMessage(string.Format("Unable to communicate with server. {0} {1}", 
		///			ex.Message, ex.StackTrace));
		///	}
		/// </example>
		public static void PerformAction(BeginAction beginAction, EndAction endAction)
		{
			EnsureNonUIThread();
			var beginResult = beginAction(null, null);
			endAction(beginResult);
		}

		/// <summary>
		/// Performs the action synchronously.
		/// </summary>
		/// <exception cref="ConcurrencyException">Occurs if the call is executed from the UI thread.</exception>
		/// <exception cref="TargetInvocationException">Occurs if the specified <code>endAction</code> 
		/// throws a <code>TargetInvocationException</code></exception>
		/// <param name="beginAction">The Begin[MethodName] method.</param>
		/// <param name="endAction">The End[MethodName] method.</param>
		/// <param name="argument1">The first argument required 
		/// by the specified <code>BeginAction</code></param>
		/// <returns>The result of the <code>EndAction</code> method.</returns>
		/// <example>
		///	try
		///	{
		///		/* Perform synchronous WCF call. */
		///		SynchronousChannelBroker.PerformAction(
		///			someService.BeginGetString, someService.EndGetString);
		///	}
		///	catch (TargetInvocationException ex)
		///	{
		///		DisplayMessage(string.Format("Unable to communicate with server. {0} {1}", 
		///			ex.Message, ex.StackTrace));
		///	}
		/// </example>
		public static void PerformAction<TActionArgument1>(
			BeginAction<TActionArgument1> beginAction,
			EndAction endAction, 
			TActionArgument1 argument1)
		{
			EnsureNonUIThread();
			var beginResult = beginAction(argument1, null, null);
			endAction(beginResult);
		}

		public static void PerformAction<TActionArgument1, TActionArgument2>(
			BeginAction<TActionArgument1, TActionArgument2> action,
			EndAction endAction, 
			TActionArgument1 argument1, TActionArgument2 argument2)
		{
			EnsureNonUIThread();
			var beginResult = action(argument1, argument2, null, null);
			endAction(beginResult);
		}

		public static void PerformAction<TActionArgument1, TActionArgument2, TActionArgument3>(
			BeginAction<TActionArgument1, TActionArgument2, TActionArgument3> action,
			EndAction endAction, 
			TActionArgument1 argument1, TActionArgument2 argument2, TActionArgument3 argument3)
		{
			EnsureNonUIThread();
			var beginResult = action(argument1, argument2, argument3, null, null);
			endAction(beginResult);
		}

		public static void PerformAction<TActionArgument1, TActionArgument2, TActionArgument3, TActionArgument4>(
			BeginAction<TActionArgument1, TActionArgument2, TActionArgument3, TActionArgument4> action,
			EndAction endAction, 
			TActionArgument1 argument1, TActionArgument2 argument2, TActionArgument3 argument3, TActionArgument4 argument4)
		{
			EnsureNonUIThread();
			var beginResult = action(argument1, argument2, argument3, argument4, null, null);
			endAction(beginResult);
		}
		#endregion

	}
}
